﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RibbonEditor.Actions;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;
using System.Reflection;

namespace RibbonEditor
{
    public class ActionsList : IXmlBuildable, IChildDeletable
    {
        XmlNode innerXmlNode;

        public List<IAction> InnerActions;

        public ActionsList()
        {
            this.InnerActions = new List<IAction>();
        }

        public ActionsList(XmlNode actionsNode)
            : this()
        {
            try
            {
                this.innerXmlNode = actionsNode;

                foreach (XmlNode node in actionsNode.ChildNodes)
                {
                    if (node.NodeType == XmlNodeType.Comment)
                        return;

                    IAction action = ActionHelper.GetIActionFromXml(node);
                    this.InnerActions.Add(action);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void AddActionsNode(XmlNode parentNode)
        {
            try
            {
                XmlNode actionsXmlNode = XmlHelper.AddNode(parentNode, "Actions");

                foreach (IAction action in this.InnerActions)
                {
                    action.AddNode(actionsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode actionsNode = new TreeNode("Actions");
                actionsNode.ContextMenuStrip = this.BuildContextMenu(actionsNode);
                actionsNode.Tag = this;

                foreach (IAction action in this.InnerActions)
                {
                    action.BuildTreeViewElement(actionsNode);
                }

                parentNode.Nodes.Add(actionsNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode actionsNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
            cms.Tag = actionsNode;
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            
            ToolStripMenuItem tsmi1 = new ToolStripMenuItem("Add JavaScript Function");
            tsmi1.Tag = "JavaScriptFunction";

            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("Add Url");
            tsmi2.Tag = "Url";

            ToolStripMenuItem tsmi3 = new ToolStripMenuItem("Add Reference to script web resource");
            tsmi3.Tag = "Reference";

            cms.Items.Add(tsmi1);
            cms.Items.Add(tsmi2);
            cms.Items.Add(tsmi3);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);

            return cms;
        }

        void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //((ContextMenuStrip)sender).Items[0].Enabled = ((TreeNode)((ContextMenuStrip)sender).Tag).Nodes.Count == 0;
            //((ContextMenuStrip)sender).Items[1].Enabled = ((TreeNode)((ContextMenuStrip)sender).Tag).Nodes.Count == 0;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

            string itemTag = e.ClickedItem.Tag.ToString();

            Assembly assembly = Assembly.GetExecutingAssembly();
            AssemblyName assemblyName = assembly.GetName();
            IAction action = null;

            switch(e.ClickedItem.Tag.ToString())
            {
                case "Url":
                    {
                        Type t = Type.GetType(assemblyName.Name + ".Actions.Url");
                        action = (IAction)Activator.CreateInstance(t);
                    }
                    break;
                case "JavaScriptFunction":
                    {
                        Type t = Type.GetType(assemblyName.Name + ".Actions.JavaScriptFunction");
                        action = (IAction)Activator.CreateInstance(t, new object[] { false });
                    }
                    break;
                case "Reference":
                    {
                        Type t = Type.GetType(assemblyName.Name + ".Actions.JavaScriptFunction");
                        action = (IAction)Activator.CreateInstance(t,new object[]{true});
                    }
                    break;
            }

            if (action != null)
            {
                InnerActions.Add(action);
                action.BuildTreeViewElement(selectedNode);
                selectedNode.TreeView.SelectedNode = selectedNode.LastNode;
            }
        }
     
        #region IXmlBuildable Members

        public void AddNode(XmlNode parentNode)
        {
            try
            {
                this.AddActionsNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is IAction)
                {
                    this.InnerActions.Remove((IAction)o);
                }
                else
                {
                    throw new Exception("Unsupported child type: " + o.GetType());
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
